// 上传图片
const vue =  new Vue({
    el: '#signupForm',
    data: {

        imgDataURL: ''

    },
    methods: {
        fileUpload: function (evt) {
            const files = evt.target.files;
            if (files.length == 0) {
                return;
            }

            const file = files[0];
            if (!/image\/\w+/.test(file.type)) {
                parent.layer.alert("请选择图像类型的文件！");
                return;
            }

            let vm = this;

            const fileType = '.' + file.name.toLowerCase().split('.').pop();

            const reader = new FileReader();
            reader.onload = (function (file) {
                return function (e) {
                    const result = e.target.result;
                    vm.imgDataURL = result;
                    const base64Str = result.split(",")[1];
                    // 上传地址动态获取
                    var uploadUrl =$("#uploadUrl").val();
                    $.post(uploadUrl, {fileType, base64Str}, function (resp) {
                        if (!!resp) {
                            const base64 = new Base64();
                            const respData = JSON.parse(base64.decode(resp));
                            if (respData.code === 0) {
                                // vm.reqData.frontImgUrl = respData.data.imgPath;
                                $("#frontImgUrl").val(respData.data.imgPath);
                            }
                        }
                    });
                }
            })(file);
            reader.readAsDataURL(file);
        }
    }

});

$().ready(function() {
    dateRangeInit();
    validateRule();
    $('.summernote').summernote({
        height : '220px',
        lang : 'zh-CN',
        callbacks: {
            onImageUpload: function(files, editor, $editable) {
                console.log("onImageUpload");
                sendFile(files);
            }
        }
    });
    var detail = $("#detail").val();

    $('#content_sn').summernote('code', detail);

});

$.validator.setDefaults({
    submitHandler : function() {
        //验证
        var daterange = $("#daterange-btn").val();
        if(daterange == null || daterange == '' || daterange == undefined ){
            parent.layer.alert("有效时间不能为空");
            return false;
        }
        save();
    }
});
function save() {
    $("#status").val(1);

    var content_sn = $("#content_sn").summernote('code');
    $("#detail").val(content_sn);
    $.ajax({
        cache : true,
        type : "POST",
        url : "/news/manage/save",
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();


            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}
function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            title: {
                required: true
            },
            summary:{
                required: true
            }
        },
        messages: {
            title: {
                required: icon + "必填"
            },
            summary: {
                required: icon + "必填"
            }
        }
    })
}

function returnList() {
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
}

//时间范围控件
function dateRangeInit() {
    //定义locale汉化插件
    var locale = {
        "format": 'YYYY-MM-DD HH:mm:ss',
        "separator": " ~ ",
        "applyLabel": "确定",
        "cancelLabel": "取消",
        "fromLabel": "起始时间",
        "toLabel": "结束时间'",
        "customRangeLabel": "自定义",
        "weekLabel": "W",
        "daysOfWeek": ["日", "一", "二", "三", "四", "五", "六"],
        "monthNames": ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        "firstDay": 0
    };
    //初始化显示当前时间
    //$('#daterange-btn span').html(moment().format('YYYY-MM-DD HH:mm:ss') + ' - ' + moment().format('YYYY-MM-DD HH:mm:ss'));
    //$('#dateRangeStart').val(moment().format('YYYY-MM-DD HH:mm:ss'));
    //$('#dateRangeEnd').val(moment().format('YYYY-MM-DD HH:mm:ss'));
    //日期控件初始化
    $('#dateRemove').click(function () {
        $('#daterange-btn').val('');
        $('#dateRangeStart').val('');
        $('#dateRangeEnd').val('');
    });
    $('#daterange-btn').daterangepicker(
        {
            'locale': locale,
            //汉化按钮部分
            /*ranges: {
                '今日': [moment(), moment()],
                '昨日': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                '最近7日': [moment().subtract(6, 'days'), moment()],
                '最近30日': [moment().subtract(29, 'days'), moment()],
                '本月': [moment().startOf('month'), moment().endOf('month')],
                '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            },*/
            startDate: moment().subtract(29, 'days'),
            endDate: moment(),
            timePicker: true,
            format: 'YYYY-MM-DD HH:mm:ss',
            timePicker24Hour: true,
            timePickerIncrement: 15,
            showDropdowns: true,
            autoUpdateInput: false,
            timePickerSeconds: true
        },
        function (start, end) {
            $('#daterange-btn').val(start.format('YYYY-MM-DD HH:mm:ss') + ' ~ ' + end.format('YYYY-MM-DD HH:mm:ss'));
            $('#dateRangeStart').val(start.format('YYYY-MM-DD HH:mm:ss'));
            $('#dateRangeEnd').val(end.format('YYYY-MM-DD HH:mm:ss'));
        }
    );
}



